var _CONFIG = {
    tBINDING_URI: base_url + 'so/project/binding',
    EDITPANEL_URI:base_url + 'so/project/editpanel',
    COMMIT_URI:base_url + 'so/excution/commit/projects/projects/proj',
    DELETE_URI:base_url + 'so/excution/delete/projects/projects/proj'
};
var _SO = (function() {
    var oTable;
    var that = this;
    return {
        createtable :   function(){
             oTable = $('#data_table').dataTable({
                aaSorting: [],
                aoColumns: [
                    {
                        mData: "proj_id",sWidth: "58px",sClass:'',
                        mRender: function ( value, type, datarow ) {
                            var str = "";
                            str += '<span class="icon-pencil" title="Edit:'+value+'" style="padding-right:12px" onclick="_SO.onEditItem(' + value + ');"></span>';
                            str += '<span class="icon-trash" title="Delete:'+value+'" style="padding-right:12px" onclick="_SO.onDeleteItem(' + value + ');"></span>';

                            return str;
                        }
                    },
                    {
                        mData: "proj_title"
                    },
                    {mData: "proj_type"},
                    {mData: "proj_concept_photo"},
                    {mData: "proj_model"},
                    {mData: "proj_makeup"},
                    {mData: "proj_stylist"},
                    {
                        mData: "proj_status",sClass:'center',
                        mRender: function ( value, type, datarow ) {
                            var str = "";
                            if(value==='true')
                                str+='<input type="checkbox" onclick="_SO.onStatusChange(this,'+datarow.proj_id+');" checked=1/>';
                            else
                                str+='<input type="checkbox" onclick="_SO.onStatusChange(this,'+datarow.proj_id+');"/>';
                            return str;
                        }
                    },
                    {mData: "proj_position",bVisible:false},
                    {mData: "proj_insert"},
                    {mData: "proj_update"}
                ],
                sServerMethod: "POST",
                iDisplayLength: 50,
                bProcessing: true,
                bServerSide: true,
                sAjaxSource: _CONFIG.tBINDING_URI,
                bJQueryUI: false,
                bAutoWidth: false,
                sPaginationType: "full_numbers",
                //sDom": '<"datatable-header"fl>t<"datatable-footer"ip>',
                oLanguage: {
                    "sSearch": "<span>Filter records:</span> _INPUT_",
                    "sLengthMenu": "<span>Show entries:</span> _MENU_",
                    "oPaginate": {"sFirst": "First", "sLast": "Last", "sNext": ">", "sPrevious": "<"}
                }
            });/*.makeEditable({
				aoColumns:[
					null,
					{},
					{
						type: 'select',onblur: 'submit',
						data: "{'':'Please select...', 'fashion':'fashion','wedding':'wedding','portrait':'portrait','commercial':'commercial'}",
						sUpdateURL: function(value, settings){
							//console.log(value);
							return value;
						},
						fnOnCellUpdated: function(sStatus, sValue, settings){
							//alert("(Cell Callback): Cell is updated with value " + sValue);
						}
					},
					{},
					{},
					{},
					{},
					null,
					null,
					null
				],
				//sUpdateURL: "UpdateData.php",
				fnOnEdited: function(status)
                {       
                  //console.log("Edit action finished. Status - " + status);
                },
				//sAddURL: "AddData.php",
				//sAddHttpMethod: "GET", //Used only on google.code live example because google.code server do not support POST request
				//sDeleteURL: "DeleteData.php"
			});*/
            oTable.fnSetFilteringDelay(2000);
        },
        onInit  :   function(){
            this.createtable();
        },
        onAddItem:function(){
            this.onEditItem(0);
        },
        onEditItem:function(Id){
            $('#viewport').hide();
            $('#_edit_panel_insite').show().html('<div class="base_loading" style="display: block;height: 60px"></div>');
            backend({
                url         :   _CONFIG.EDITPANEL_URI,
                data        :   {
                    Id  :   Id
                },
                callback    :   function(rsdata){
                    if(rsdata.result<0){
                        bckdialog({type:'error'}).open(rsdata.message);
                    }else{
                        $('#_edit_panel_insite').html(rsdata.htmlreponse);
                        //$('#proj_path').slugify('#proj_title');
                        $("#_form_edit_panel").validationEngine({validateNonVisibleFields:true,scroll:false});
                    }
                }
            }).call();
        },
        onSave:function(){
            if( $('#_form_edit_panel').validationEngine('validate') === false)return false;
            if($('#_form_edit_panel .base_photo img').length==0){
                $.jGrowl('Project photo must be none empty',{ sticky: true, theme: 'growl-error', header: 'Error!' });
                return;
            }
            var Params=$('#_form_edit_panel').serializeObject();
            Params.proj_images='';
            $('#_form_edit_panel .base_photo img').map(function() {
                //return this.src;
                Params.proj_images+=this.src+'\n';
            });
            new backend({
                url         :   _CONFIG.COMMIT_URI,
                data        :   {
                    Params  :   Params,
                    Id      :   $('#proj_id').val()
                },
                callback    :   function(rsdata){
                    if(rsdata.result<0){
                        bckdialog({type:'error'}).open(rsdata.message);
                    }else{
                        $('#_edit_panel_insite').hide();
                        $('#viewport').show();
                        oTable.fnDraw(false);
                        $.jGrowl(rsdata.message);
                    }
                    
                }
            }).call();
            return false;
        },
        onCancel:function(){
            $('#_edit_panel_insite').hide();
            $('#viewport').show();
        },
        onBrowsePath:function(){
            openKCFinderMulti(function(files){
                //var Images=new Array();
                var str='';
                for (var i = 0; i < files.length; i++){
                    //Images[i]=files[i];
                    $('#add_photo_to_project').before("<div class='base_photo'><img src='"+files[i]+"'/><div class='delete_photo' onclick='destroy_parent(this);'></div></div>");
                    //str+=files[i]+'\n';
                }
                //$('#proj_images').val($('#proj_images').val()+str);
            });
        },
        onGetPath:function(){
            
        },
        onDeleteItem:function(Id){
            backend_delete(_CONFIG.DELETE_URI,Id,function(){
                _SO.onRefresh();
            });
        },
        onRefresh:function(){
            oTable.fnDraw(false);
        },
        onFillter:function(v){
            oTable.fnFilter(v, 2);
        },
        onStatusChange:function(that,Id){
            var _status = $(that).is(':checked')?'true':'false';
            new backend({
                url         :   _CONFIG.COMMIT_URI,
                data        :   {
                    Params  :   {
                        proj_status:_status
                    },
                    Id      :   Id
                },
                callback    :   function(rsdata){
                    if(rsdata.result<0){
                        bckdialog({type:'error'}).open(rsdata.message);
                    }
                }
            }).call();
        }
    };
}());